package com.lyf.boot.modules.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.lyf.boot.annotation.Log;
import com.lyf.boot.core.model.IdArrDto;
import com.lyf.boot.core.model.PageInfo;
import com.lyf.boot.core.model.R;
import com.lyf.boot.enums.BusinessType;
import com.lyf.boot.modules.system.entity.dto.LoginRecordPageDto;
import com.lyf.boot.modules.system.entity.vo.LoginRecordVo;
import com.lyf.boot.modules.system.service.ILoginRecordService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 登录日志
 * @author Luyufan
 * @date 2023/8/24 15:55
 */
@RestController
@RequestMapping("/loginRecord")
@RequiredArgsConstructor
public class LoginRecordController {

    private final ILoginRecordService loginRecordService;

    /**
     * 分页查询登录日志
     * @author Luyufan
     * @date 2023/8/24 16:05
     * @param dto
     * @return R<PageInfo < LoginRecordVo>>
     */
    @SaCheckPermission("sys:loginRecord:list")
    @PostMapping("/getLoginRecordPage")
    public R<PageInfo<LoginRecordVo>> getLoginRecordPage(@RequestBody LoginRecordPageDto dto){
        return R.ok(loginRecordService.getLoginRecordPage(dto));
    }

    /**
     * 删除登录日志
     * @param dto
     * @return
     */
    @SaCheckPermission("sys:loginRecord:delete")
    @PostMapping("/deleteLoginRecord")
    @Log(module = "登录日志", business = "删除日志", businessType = BusinessType.DELETE)
    public R<Void> deleteLoginRecord(@RequestBody @Valid IdArrDto dto){
        loginRecordService.deleteLoginRecord(dto.getIds());
        return R.ok();
    }

    /**
     * 清除操作日志
     * @return
     */
    @SaCheckPermission("sys:loginRecord:clear")
    @PostMapping("/clearLoginRecord")
    @Log(module = "登录日志", business = "清除日志", businessType = BusinessType.CLEAR)
    public R<Void> clearSysOperLog(){
        loginRecordService.clearLoginRecord();
        return R.ok();
    }
}
